//
//  HomeViewController.m
//  JewelryStore
//
//  Created by lcs on 16/10/7.
//  Copyright © 2016年 com. All rights reserved.
//

#import "HomeViewController.h"
#import "HomeScrollView.h"
#import "HomeFirstCollectionViewCell.h"
#import "HomeSecondCollectionViewCell.h"
#import "HomeThirdCollectionViewCell.h"
#import "UINavigationBar+Awesome.h"

#import "HomeTitleHeadCollectView.h"

#define HomeFirstCell @"homeFirstCell"
#define HomeSecondCell @"homeSecondCell"
#define HomeThirdCell @"homeThirdCell"

#define HomeTopHead @"homeTopHead"
#define HomeButtonHead @"homeButtonHead"
#define HomeTitleHead @"homeTitleHead"

#define CLCLE_HEIGHT SCREEN_HEIGHT/3.2
@interface HomeViewController ()<UISearchBarDelegate, SDCycleScrollViewDelegate, UICollectionViewDelegate, UICollectionViewDataSource, UICollectionViewDelegateFlowLayout>
@property (nonatomic, strong) UICollectionView *homeCollectionView;

@property (nonatomic, copy) NSArray *topImageArr;  //轮播图
@property (nonatomic, copy) NSArray *firstItems;  //第一个区数据
@property (nonatomic, copy) NSArray *secondItems; //精彩栏目
@property (nonatomic, copy) NSArray *thirdItems;  //热销推荐
@end

@implementation HomeViewController

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    [self initTopUI];
}

- (void)viewWillDisappear:(BOOL)animated
{
    [super viewWillDisappear:animated];
    [self.navigationController.navigationBar setTintColor:[UIColor lightGrayColor]];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    [self initData];
    [self initUI];
}

#pragma mark - initUI

- (void)initTopUI
{
    //设置navigation透明
    [self.navigationController.navigationBar setBackgroundImage:[UIImage new] forBarMetrics:UIBarMetricsDefault];
    [self.navigationController.navigationBar setShadowImage:[UIImage new]];
    
    //设置tint颜色
    [self.navigationController.navigationBar setTintColor:kWhite];
    
    //LCSlogo
    UIBarButtonItem *logo = [[UIBarButtonItem alloc] initWithImage:KImageNamed(@"logo_normal") style:UIBarButtonItemStyleDone target:self action:@selector(logoClick)];
    
    //搜索框
    UIImageView *search = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH/1.1, 25)];
    search.image = KImageNamed(@"search");
    search.userInteractionEnabled = YES;
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self
                                                                          action:@selector(searchTap:)];
    [search addGestureRecognizer:tap];
    
    //消息
    UIBarButtonItem *message = [[UIBarButtonItem alloc] initWithImage:KImageNamed(@"message") style:UIBarButtonItemStyleDone target:self action:@selector(messageClick)];
    
    self.navigationItem.leftBarButtonItem = logo;
    self.navigationItem.rightBarButtonItem = message;
    self.navigationItem.titleView = search;

    [self.navigationController.navigationBar lt_setBackgroundColor:[UIColor clearColor]];
}

- (void)initUI
{
    [self.view addSubview:self.homeCollectionView];
    self.automaticallyAdjustsScrollViewInsets = NO;
    
    //上滚按钮
    UIButton *scrollTopBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    scrollTopBtn.frame = CGRectMake(SCREEN_WIDTH-60, SCREEN_HEIGHT-110, 50, 50);
    [scrollTopBtn addTarget:self action:@selector(scrollTop) forControlEvents:UIControlEventTouchUpInside];
    [scrollTopBtn setBackgroundImage:KImageNamed(@"scrollToTop") forState:UIControlStateNormal];
    [self.view addSubview:scrollTopBtn];
}

#pragma mark - initData

- (void)initData
{
    _topImageArr = @[@"http://pic.4j4j.cn/upload/pic/20130815/31e652fe2d.jpg", @"http://pic.4j4j.cn/upload/pic/20130909/681ebf9d64.jpg", @"http://img3.imgtn.bdimg.com/it/u=581864004,1701839937&fm=21&gp=0.jpg", @"http://pic27.nipic.com/20130219/4499633_230915004000_2.jpg"];
    
    _firstItems = @[@{@"logo":@"first_1", @"title":@"黄金"},
                    @{@"logo":@"first_2", @"title":@"钻石"},
                    @{@"logo":@"first_3", @"title":@"戒指"},
                    @{@"logo":@"first_4", @"title":@"吊坠"},
                    @{@"logo":@"first_5", @"title":@"手链"},
                    @{@"logo":@"first_6", @"title":@"项链"},
                    @{@"logo":@"first_7", @"title":@"定制"},
                    @{@"logo":@"first_8", @"title":@"更多"}];
    
    _secondItems = @[@{@"image":@"jclm_1"},
                     @{@"image":@"jclm_2"},
                     @{@"image":@"jclm_3"},
                     @{@"image":@"jclm_4"}];
    
    _thirdItems = @[@{@"image":@"shop", @"description":@"加价电费健康减肥的打飞机", @"price":@"100", @"isHave":@"1", @"isTry":@"0"},
                    @{@"image":@"shop", @"description":@"可立即按对方就看见", @"price":@"99", @"isHave":@"0", @"isTry":@"1"},
                    @{@"image":@"shop", @"description":@"加多少房间卡大", @"price":@"5445", @"isHave":@"1", @"isTry":@"1"},
                    @{@"image":@"shop", @"description":@"就可立即按对方阿卡发都是", @"price":@"645", @"isHave":@"1", @"isTry":@"0"},
                    @{@"image":@"shop", @"description":@"健康就挨打快递放假都是点击", @"price":@"5745", @"isHave":@"0", @"isTry":@"1"},
                    @{@"image":@"shop", @"description":@"借记卡代发爱打飞机开房大厦", @"price":@"5778", @"isHave":@"1", @"isTry":@"1"},
                    @{@"image":@"shop", @"description":@"价代发快点发发的卡房间爱对方", @"price":@"8656", @"isHave":@"0", @"isTry":@"0"},
                    @{@"image":@"shop", @"description":@"家酒店房间爱发到付件是否", @"price":@"54646", @"isHave":@"1", @"isTry":@"1"},
                    @{@"image":@"shop", @"description":@"是否发放的发生发生的大幅", @"price":@"45645", @"isHave":@"0", @"isTry":@"1"},
                    @{@"image":@"shop", @"description":@"发大发发打发阿打发打发发", @"price":@"5648", @"isHave":@"1", @"isTry":@"1"},
                    @{@"image":@"shop", @"description":@"打算让我去让他天天啊", @"price":@"5688", @"isHave":@"1", @"isTry":@"0"},
                    @{@"image":@"shop", @"description":@"是否会有意外其他我", @"price":@"4777", @"isHave":@"1", @"isTry":@"0"},
                    @{@"image":@"shop", @"description":@"温热通天塔一天也一样一样", @"price":@"4567", @"isHave":@"0", @"isTry":@"1"},
                    @{@"image":@"shop", @"description":@"案发地方成为二人", @"price":@"4589", @"isHave":@"1", @"isTry":@"1"},
                    @{@"image":@"shop", @"description":@"啊发发我让他任天堂", @"price":@"7899", @"isHave":@"1", @"isTry":@"0"},
                    ];
}

#pragma mark - 懒加载

- (UICollectionView *)homeCollectionView
{
    if (!_homeCollectionView) {
        
        //布局
        UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];

        _homeCollectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT-45) collectionViewLayout:layout];
        _homeCollectionView.dataSource = self;
        _homeCollectionView.delegate = self;
        _homeCollectionView.backgroundColor = kBackgroundGray;
        _homeCollectionView.showsVerticalScrollIndicator = NO;
        
        //注册item
        [_homeCollectionView registerNib:[UINib nibWithNibName:@"HomeFirstCollectionViewCell" bundle:nil] forCellWithReuseIdentifier:HomeFirstCell];
        [_homeCollectionView registerNib:[UINib nibWithNibName:@"HomeSecondCollectionViewCell" bundle:nil] forCellWithReuseIdentifier:HomeSecondCell];
        [_homeCollectionView registerNib:[UINib nibWithNibName:@"HomeThirdCollectionViewCell" bundle:nil] forCellWithReuseIdentifier:HomeThirdCell];
        
        //注册headView
        [_homeCollectionView registerClass:[UICollectionReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:HomeTopHead];
        [_homeCollectionView registerClass:[UICollectionReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:HomeButtonHead];
        [_homeCollectionView registerNib:[UINib nibWithNibName:@"HomeTitleHeadCollectView" bundle:nil] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:HomeTitleHead];
        
    }
    return _homeCollectionView;
}

#pragma mark - barBarButtonClick(导航栏点击)

- (void)logoClick
{

}

- (void)messageClick
{

}

#pragma mark - searchBar(搜索点击)

- (BOOL)searchBarShouldBeginEditing:(UISearchBar *)searchBar
{
    UIViewController *vc = [[UIViewController alloc] init];
    vc.view.backgroundColor = kWhite;
    [self.navigationController pushViewController:vc animated:YES];
    return YES;
}

#pragma mark - SDCycleScrollViewDelegate

- (void)cycleScrollView:(SDCycleScrollView *)cycleScrollView didSelectItemAtIndex:(NSInteger)index
{
    [[UIApplication sharedApplication] openURL:[NSURL URLWithString:@"http://www.baidu.com"]];
}

#pragma mark - collectionView

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    switch (section) {
        case 0:
            return 8;
            break;
        case 1:
            return 0;
            break;
        case 2:
            return 4;
            break;
        case 3:
            return _thirdItems.count;
            break;
        default:
            return 0;
            break;
    }
}

- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView
{
    return 4;
}

- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath
{
    switch (indexPath.section) {
        case 0:
            return CGSizeMake(SCREEN_WIDTH/4, SCREEN_WIDTH/4.5);
            break;
        case 1:
            return CGSizeZero;
            break;
        case 2:
            return CGSizeMake(SCREEN_WIDTH/2.08, 80);
            break;
        case 3:
            return CGSizeMake(SCREEN_WIDTH/2.08, SCREEN_WIDTH/1.7);
            break;
            
        default:
            return CGSizeZero;
            break;
    }
}

- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout minimumInteritemSpacingForSectionAtIndex:(NSInteger)section
{
    return 0;
}

- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout minimumLineSpacingForSectionAtIndex:(NSInteger)section
{
    //第一区返回5
//    return section == 0 ? 5 : 0;
    return -2.5;
}

- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout referenceSizeForHeaderInSection:(NSInteger)section
{
    if (section == 0) {
        return CGSizeMake(SCREEN_WIDTH, CLCLE_HEIGHT);
    }else if (section == 1)
    {
        return CGSizeMake(SCREEN_WIDTH, 90);

    }else
    {
        return CGSizeMake(SCREEN_WIDTH, 15);
    }
}

- (UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout insetForSectionAtIndex:(NSInteger)section
{
    if (section == 2 || section == 3) {
        return UIEdgeInsetsMake(5, 5, 0, 5);
    }
    return UIEdgeInsetsZero;
}
- (__kindof UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    UICollectionViewCell *cell;
    switch (indexPath.section) {
        case 0:
        {
            //8个items
            HomeFirstCollectionViewCell *firstCell = [collectionView dequeueReusableCellWithReuseIdentifier:HomeFirstCell forIndexPath:indexPath];
                firstCell.model = _firstItems[indexPath.row];
                cell = firstCell;
        }
            
            break;
        case 1:
        {
            //广告items（未显示，不起作用）
            HomeFirstCollectionViewCell *firstCell = [collectionView dequeueReusableCellWithReuseIdentifier:HomeFirstCell forIndexPath:indexPath];
            cell = firstCell;
        }

            break;
        case 2:
        {
            //精选items
            HomeSecondCollectionViewCell *firstCell = [collectionView dequeueReusableCellWithReuseIdentifier:HomeSecondCell forIndexPath:indexPath];
            firstCell.model = _secondItems[indexPath.row];
            cell = firstCell;
        }

            break;
        case 3:
        {
            //热销items
            HomeThirdCollectionViewCell *firstCell = [collectionView dequeueReusableCellWithReuseIdentifier:HomeThirdCell forIndexPath:indexPath];
            [firstCell reset];    //重置cell的小图标
            firstCell.model = _thirdItems[indexPath.row];
            cell = firstCell;
        }
            break;
            
        default:
            break;
    }
    
    return cell;
}

- (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath
{
    UICollectionReusableView *result;
    if ([kind isEqualToString:UICollectionElementKindSectionHeader]) {
        switch (indexPath.section) {
            case 0:
            {
                //顶部轮播
                result = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:HomeTopHead forIndexPath:indexPath];
                HomeScrollView *sc = [[HomeScrollView alloc] initWithFrame:CGRectMake(0, 0, CGRectGetWidth(self.view.frame), CLCLE_HEIGHT) imageURLStringsGroup:_topImageArr delegate:self isAutoScroll:YES];
                [result addSubview:sc];

            }
                break;
            case 1:
            {
                //广告
                result = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:HomeButtonHead forIndexPath:indexPath];
                UIButton *advertBtn= [UIButton buttonWithType:UIButtonTypeCustom];
                [advertBtn setBackgroundImage:KImageNamed(@"advertisting") forState:UIControlStateNormal];
                [advertBtn addTarget:self action:@selector(advertClick) forControlEvents:UIControlEventTouchUpInside];
                advertBtn.frame = CGRectMake(0, 0, SCREEN_WIDTH, 80);
                [result addSubview:advertBtn];

            }
                break;
            case 2:
            {
                //精选
                HomeTitleHeadCollectView *head = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:HomeTitleHead forIndexPath:indexPath];
                head.titleImageView.image = KImageNamed(@"wonderful");
                result = head;

            }
                break;
            case 3:
            {
                //热销
                HomeTitleHeadCollectView *head = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:HomeTitleHead forIndexPath:indexPath];
                head.titleImageView.image = KImageNamed(@"recommend");
                result = head;

            }
                break;
            default:
                break;
        }
        
        
    }
    return result;
}

#pragma mark - scrollViewDelegate

- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
    CGFloat offsetY = scrollView.contentOffset.y;
    if (offsetY > 0) {
        CGFloat alpha = MIN(1, 1 - (64 - offsetY) / 64);
        [self.navigationController.navigationBar lt_setBackgroundColor:[kBackgroundGray colorWithAlphaComponent:alpha]];
    } else {
        [self.navigationController.navigationBar lt_setBackgroundColor:[kBackgroundGray colorWithAlphaComponent:0]];
    }
}


#pragma mark - private

//广告
- (void)advertClick
{
    [[UIApplication sharedApplication] openURL:[NSURL URLWithString:@"http://www.baidu.com"]];
}

//置顶
- (void)scrollTop
{
    [self.homeCollectionView setContentOffset:CGPointZero animated:YES];
}

//go to search
- (void)searchTap:(UITapGestureRecognizer *)tap
{
    TT_Log(@"go to search");
}
@end
